package cn.itsource.org.service.impl;

import cn.itsource.base.service.impl.BaseServiceImpl;
import cn.itsource.org.domain.Department;
import cn.itsource.org.mapper.DepartmentMapper;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import cn.itsource.base.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service  /*注册到spring  交给spring管理*/
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl  extends BaseServiceImpl<Department> implements IDepartmentService {


    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    //@Transactional(readOnly = false,propagation = Propagation.REQUIRED) /*不是只读，如果没有事务，创建事务*/
    @Transactional
    public void add(Department department) {

        departmentMapper.save(department);//保存之后，获取到生成主键

        //如果没有选则父级部门  拿到自己生成的id   department.setDirPath("/"+department.getId());
        if(department.getParent()==null){
            department.setDirPath("/"+department.getId());
        }else{
            //如果有选中父级部门： 拿到父级的dirpath
            Long pid = department.getParent().getId();
            Department parent = departmentMapper.findOne(pid);
            //department.setDirPath("dirpath"+department.getId());
            department.setDirPath(parent.getDirPath()+"/"+department.getId());
        }

        //维护到了dirpath  更新这条数据
        departmentMapper.update(department);





    }

    @Override
    @Transactional
    public void update(Department department) {
        departmentMapper.update(department);
    }
    /*批量删除*/
    @Override
    public void patch(List<Long> ids) {
        departmentMapper.patch(ids);
    }

    @Override
    public List<Department> tree() {
        return departmentMapper.tree();
    }


}
